package client;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.Timer;

import util.MyParser;
import util.Point;
import util.PointList;
import javax.swing.JScrollPane;
import javax.swing.plaf.metal.MetalIconFactory;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JRadioButton;

public class GUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JMenuBar jJMenuBar = null;
	private JMenu meny = null;
	private JMenu about = null;
	private static String username = null; 
	public static JPanel sketchArea = null; 
	public static JTextArea chatHistory = null;
	private JScrollPane jScrollPane = null;
	public static JTextField chatField = null;
	private JButton jButton = null;

	private JMenuItem jMenuItem = null;
	private JMenuItem newGame = null;
	private JMenuItem joinGame = null;
	private JMenuItem startGame = null;
	private static JMenuItem clear = null;
	private JMenuItem exit = null;
	private static JLabel paintArea = null;
	private JLabel chat = null;
	private static GuiListener gl;
	private static Graphics g;
	private JMenuItem getGames = null;
	private Font font = new Font("Verdana", Font.PLAIN, 12);
	public static JTextArea scoreBoard = null;
	private JLabel scores = null;
	private static Color color = Color.black;
	private static Color backgroundColor = new Color(66, 33, 66);
	private static Color fg = new Color(255, 255, 99);
	private static int timeCount = 100;

	private static JTextField timer = null;
	private JLabel timeLabel = null;
	private JLabel currentWord = null;
	private static JTextField theWord = null;
	private static JRadioButton redbutt = null;
	private static JRadioButton bluebutt = null;
	private static JRadioButton greenbutt = null;
	private static JRadioButton blackbutt = null;
	private ButtonGroup bg = new ButtonGroup(); // @jve:decl-index=0:
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;
	private JLabel jLabel2 = null;
	private static ActionListener time = new ActionListener() {
		@Override
		public void actionPerformed(ActionEvent e) {
			if (timeCount >= 0) {
				timer.setText(Integer.toString(timeCount)); // @jve:decl-index=0:
				timeCount--;
			} else {
				timer.setText("Timeout!");
			}
		}
	};

	private static Timer t = new Timer(1000, time); // @jve:decl-index=0:

	private JLabel jLabel3 = null;
	private JMenuItem leavegame = null;

	public GUI(GuiListener gl) {
		super();
		this.gl = gl;
		initialize();
	}

	private void initialize() {
		this.setSize(1053, 708);
		this.setResizable(false);
		this.setJMenuBar(getJJMenuBar());
		this.setContentPane(getJContentPane());
		this.setTitle("Pictionary");
		bg.add(getRedbutt());
		bg.add(getGreenbutt());
		bg.add(getBluebutt());
		bg.add(getBlackbutt());
		while (username == null || username.equals("")) {

			username = (String) JOptionPane.showInputDialog(GUI.sketchArea,
					"Enter your username", "Name",
					JOptionPane.QUESTION_MESSAGE, MetalIconFactory
							.getTreeComputerIcon(), null, "");
			if (username == null) {
				System.exit(1);
			}
		}
		gl.sendGame("name" + MyParser.FIELD + username);
		setUsername(username);

	}

	private JPanel getJContentPane() {
		if (jContentPane == null) {

			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(41, 488, 43, 22));
			jLabel3.setText("Black");
			jLabel3.setForeground(fg);
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(188, 484, 58, 31));
			jLabel2.setText("Blue");
			jLabel2.setForeground(fg);
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(138, 487, 80, 25));
			jLabel1.setText("Green");
			jLabel1.setForeground(fg);
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(91, 487, 31, 24));
			jLabel.setText("Red");
			jLabel.setForeground(fg);
			currentWord = new JLabel();
			currentWord.setBounds(new Rectangle(242, 612, 122, 17));
			currentWord.setText("Current word:");
			currentWord.setForeground(fg);
			timeLabel = new JLabel();
			timeLabel.setBounds(new Rectangle(11, 608, 128, 24));
			timeLabel.setText("Time remaining:");
			timeLabel.setForeground(fg);
			scores = new JLabel();
			scores.setForeground(fg);
			scores.setBounds(new Rectangle(896, 11, 57, 20));
			scores.setText("Scores");
			chat = new JLabel();
			chat.setForeground(fg);
			chat.setBounds(new Rectangle(885, 257, 78, 16));
			chat.setText("Chat here");
			paintArea = new JLabel();
			paintArea.setForeground(fg);
			paintArea.setBounds(new Rectangle(370, 6, 300, 16));
			jContentPane = new JPanel();
			jContentPane.setLayout(null);

			jContentPane.add(getSketchArea(), null);
			jContentPane.add(getChatField(), null);
			jContentPane.add(getJButton(), null);
			jContentPane.add(paintArea, null);
			jContentPane.add(chat, null);
			jContentPane.add(getJScrollPane(), null);
			jContentPane.add(getScoreBoard(), null);
			jContentPane.add(scores, null);
			jContentPane.add(getTimer(), null);
			jContentPane.add(timeLabel, null);
			jContentPane.add(currentWord, null);
			jContentPane.add(getTheWord(), null);
			jContentPane.add(getRedbutt(), null);
			jContentPane.add(getBluebutt(), null);
			jContentPane.add(getGreenbutt(), null);

			jContentPane.add(jLabel, null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(getBlackbutt(), null);
			jContentPane.add(jLabel3, null);
			jContentPane.addKeyListener(gl);
			jContentPane.setBackground(backgroundColor);
		}
		return jContentPane;
	}

	private JMenuBar getJJMenuBar() {
		if (jJMenuBar == null) {
			jJMenuBar = new JMenuBar();
			jJMenuBar.add(getJMenu());
			jJMenuBar.add(getJMenu1());
		}
		return jJMenuBar;
	}

	private JMenu getJMenu() {
		if (meny == null) {
			meny = new JMenu();
			meny.setText("Menyn");
			meny.add(getStartGame());
			meny.add(getJoinGame());
			meny.add(getNewGame());
			meny.add(getClear());
			meny.add(getExit());

			meny.add(getLeavegame());
		}
		return meny;
	}

	private JMenu getJMenu1() {
		if (about == null) {
			about = new JMenu();
			about.setText("About");
			about.setToolTipText("Read This");
			about.add(getJMenuItem());

		}
		return about;
	}

	private JMenuItem getJMenuItem() {
		if (jMenuItem == null) {
			jMenuItem = new JMenuItem();
			jMenuItem.setText("Read This");
			jMenuItem.addActionListener(gl);
			jMenuItem.setActionCommand("about");
		}
		return jMenuItem;
	}

	private JMenuItem getStartGame() {
		if (startGame == null) {
			startGame = new JMenuItem();
			startGame.setText("Start game");
			startGame.setActionCommand("start");
			startGame.addActionListener(gl);
		}
		return startGame;
	}

	private JMenuItem getNewGame() {
		if (newGame == null) {
			newGame = new JMenuItem();
			newGame.setText("New game");
			newGame.setActionCommand("new");
			newGame.addActionListener(gl);
		}
		return newGame;
	}

	private JMenuItem getJoinGame() {
		if (joinGame == null) {
			joinGame = new JMenuItem();
			joinGame.setText("Join game");
			joinGame.setActionCommand("join");
			joinGame.addActionListener(gl);
		}
		return joinGame;
	}

	private JMenuItem getExit() {
		if (exit == null) {
			exit = new JMenuItem();
			exit.setText("Exit");
			exit.setActionCommand("exit");
			exit.addActionListener(gl);
		}
		return exit;
	}

	private JMenuItem getClear() {
		if (clear == null) {
			clear = new JMenuItem();
			clear.setText("Clear");
			clear.setActionCommand("clear");
			clear.addActionListener(gl);
			clear.setEnabled(false);
		}
		return clear;
	}

	private JPanel getSketchArea() {
		if (sketchArea == null) {
			sketchArea = new JPanel();
			sketchArea.setPreferredSize(new Dimension(200, 1));
			sketchArea.setForeground(new Color(51, 100, 51));
			sketchArea.setBorder(BorderFactory.createLineBorder(Color.gray, 5));
			sketchArea.setBounds(new Rectangle(17, 45, 764, 392));
			sketchArea.setEnabled(false);
			sketchArea.setBackground(Color.white);

		}
		return sketchArea;
	}

	public static String getUsername() {
		return username;
	}

	public static void setPaintMaster(boolean enabled) {
		if (enabled) {
			sketchArea.addMouseMotionListener(gl);
			sketchArea.addMouseListener(gl);
			sketchArea.setBorder(BorderFactory.createLineBorder(Color.red, 5));
			GUI.setColor(Integer.toString((Color.black.getRGB())), "black");
		} else {
			sketchArea.removeMouseListener(gl);
			sketchArea.removeMouseMotionListener(gl);
			sketchArea.setBorder(BorderFactory.createLineBorder(Color.gray, 5));

		}
		clear.setEnabled(enabled);
	}

	public static void startTime(int tc) {
		timeCount = tc;
		t.stop();
		t.start();

	}

	public static void stopTime() {
		t.stop();
	}

	private JTextArea getChatHistory() {
		if (chatHistory == null) {
			chatHistory = new JTextArea();
			chatHistory.setBounds(new Rectangle(555, 46, 172, 223));
			chatHistory.setEditable(false);
			chatHistory.setFont(font);
			// chatHistory.setForeground(Color.red);

		}
		return chatHistory;
	}

	private JTextField getChatField() {
		if (chatField == null) {
			chatField = new JTextField();
			chatField.setBounds(new Rectangle(819, 535, 210, 29));
			chatField.addKeyListener(gl);
		}
		return chatField;
	}

	private JButton getJButton() {
		if (jButton == null) {
			jButton = new JButton();
			jButton.setBounds(new Rectangle(888, 579, 73, 29));
			jButton.setText("Send");
			jButton.addActionListener(gl);
			jButton.setActionCommand("send");

		}
		return jButton;
	}

	public static String getColor() {
		return Integer.toString(color.getRGB());
	}

	public static void setColor(String c) {
		color = new Color(Integer.parseInt(c));
	}

	public static void setColor(String c, String name) {
		color = new Color(Integer.parseInt(c));
		if (name == "black") {
			blackbutt.setSelected(true);
		}
		if (name == "blue") {
			bluebutt.setSelected(true);
		}
		if (name == "red") {
			redbutt.setSelected(true);
		}
		if (name == "green") {
			greenbutt.setSelected(true);
		}

	}

	public static void drawList(PointList pl) {

		g = sketchArea.getGraphics();
		g.setColor(color);
		Point prev = pl.get(0);
		Point curr = null;

		int i = 1;
		while (i < pl.size() - 1) {
			if (pl.get(i).getX() != -1) {
				curr = pl.get(i);
				g.drawLine(prev.getX(), prev.getY(), curr.getX(), curr.getY());
				prev = curr;
			} else {
				i++;
				prev = pl.get(i);
			}
			i++;
		}
	}

	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane(getChatHistory());
			jScrollPane.setBounds(new Rectangle(818, 286, 212, 223));
			jScrollPane
					.setBorder(BorderFactory.createLineBorder(Color.gray, 5));

		}
		return jScrollPane;
	}

	private JMenuItem getGetGames() {
		if (getGames == null) {
			getGames = new JMenuItem();
			getGames.setText("Show available games");
			getGames.addActionListener(gl);
			getGames.setActionCommand("getgames");
		}
		return getGames;
	}

	/**
	 * This method initializes scoreBoard
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextArea getScoreBoard() {
		if (scoreBoard == null) {
			scoreBoard = new JTextArea();
			scoreBoard.setBounds(new Rectangle(824, 50, 200, 187));
			scoreBoard.setEditable(false);
			scoreBoard.setBorder(BorderFactory.createLineBorder(Color.gray, 5));
		}
		return scoreBoard;
	}

	/**
	 * This method initializes timer
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTimer() {
		if (timer == null) {
			timer = new JTextField();
			timer.setBounds(new Rectangle(139, 607, 42, 27));
			timer.setEditable(false);
			timer.setText("");
		}
		return timer;
	}

	/**
	 * This method initializes theWord
	 * 
	 * @return javax.swing.JTextField
	 */
	private JTextField getTheWord() {
		if (theWord == null) {
			theWord = new JTextField();
			theWord.setBounds(new Rectangle(358, 607, 108, 26));
			theWord.setEditable(false);
		}
		return theWord;
	}

	public static void setWord(String s) {
		theWord.setText(s);
	}

	/**
	 * This method initializes jRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getRedbutt() {
		if (redbutt == null) {
			redbutt = new JRadioButton();
			redbutt.setBounds(new Rectangle(96, 454, 21, 21));
			redbutt.addActionListener(gl);
			redbutt.setActionCommand("red");
			redbutt.setBackground(backgroundColor);

		}
		return redbutt;
	}

	/**
	 * This method initializes jRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getBluebutt() {
		if (bluebutt == null) {
			bluebutt = new JRadioButton();
			bluebutt.setBounds(new Rectangle(192, 455, 21, 21));
			bluebutt.addActionListener(gl);
			bluebutt.setActionCommand("blue");
			bluebutt.setBackground(backgroundColor);

		}
		return bluebutt;
	}

	/**
	 * This method initializes jRadioButton
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getGreenbutt() {
		if (greenbutt == null) {
			greenbutt = new JRadioButton();
			greenbutt.setBounds(new Rectangle(145, 455, 35, 21));
			greenbutt.addActionListener(gl);
			greenbutt.setActionCommand("green");
			greenbutt.setBackground(backgroundColor);
		}
		return greenbutt;
	}

	/**
	 * This method initializes Blackbutt
	 * 
	 * @return javax.swing.JRadioButton
	 */
	private JRadioButton getBlackbutt() {
		if (blackbutt == null) {
			blackbutt = new JRadioButton();
			blackbutt.setBounds(new Rectangle(52, 455, 21, 21));
			blackbutt.addActionListener(gl);
			blackbutt.setActionCommand("black");
			blackbutt.setSelected(true);
			blackbutt.setBackground(backgroundColor);
		}
		return blackbutt;
	}

	public static void setUsername(String name) {
		username = name;
		paintArea.setText("Your name is: " + username);

	}

	/**
	 * This method initializes leavegame
	 * 
	 * @return javax.swing.JMenuItem
	 */
	private JMenuItem getLeavegame() {
		if (leavegame == null) {
			leavegame = new JMenuItem();
			leavegame.setText("Leave game");
			leavegame.addActionListener(gl);
			leavegame.setActionCommand("leavegame");
		}
		return leavegame;
	}

}
